

using System.Diagnostics;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace QNLua.Condition
{
	[JsonConverter(typeof(ConditionConverter))]
	public class Condition
	{
		public string? LeftValueStr { get; set; }
		public string? OperatorStr { get; set; }
		public object? RightValue { get; set; }
		public bool RightIsExpression { get; set; }

		// public override string ToString()
		// {
		//     return LeftValueStr + " " + OperatorStr + " " + RightValue;
		// }
	}

	public class CompoundCondition
	{
		public List<Condition> Conditions { get; set; }
		public List<ConditionOr> ConditionOrs { get; set; }
		public List<ConditionAnd> ConditionAnds { get; set; }
		public List<ConditionNot> ConditionNots { get; set; }
		public List<ConditionNotAny> ConditionNotAnys { get; set; }
		public List<ConditionNotAll> ConditionNotAlls { get; set; }

		public CompoundCondition()
		{
			Conditions = new List<Condition>();
			ConditionOrs = new List<ConditionOr>();
			ConditionAnds = new List<ConditionAnd>();
			ConditionNots = new List<ConditionNot>();
			ConditionNotAnys = new List<ConditionNotAny>();
			ConditionNotAlls = new List<ConditionNotAll>();
		}
	}


	[JsonConverter(typeof(ConditionOrConverter))]
	public class ConditionOr : CompoundCondition
	{

	}

	[JsonConverter(typeof(ConditionAndConverter))]
	public class ConditionAnd : CompoundCondition
	{
	}
	[JsonConverter(typeof(ConditionNotConverter))]
	public class ConditionNot : CompoundCondition
	{
	}

	[JsonConverter(typeof(ConditionNotAnyConverter))]
	public class ConditionNotAny : CompoundCondition
	{
	}

	[JsonConverter(typeof(ConditionNotAllConverter))]
	public class ConditionNotAll : CompoundCondition
	{
	}

	public class ConditionConverter : JsonConverter<Condition>
	{
		public override Condition? ReadJson(JsonReader reader, Type objectType, Condition? existingValue, bool hasExistingValue, JsonSerializer serializer)
		{
			var array = JArray.Load(reader);

			string? LeftValueStr = null;
			string? OperatorStr = null;
			object? RightValue = null;
			bool RightIsExpression = false;


			if (array.Count > 0)
			{
				LeftValueStr = array[0]?.ToString();
			}
			if (array.Count > 1)
			{
				OperatorStr = array[1]?.ToString();
			}
			if (array.Count > 2)
			{
				RightValue = array[2]?.ToObject<object>();
			}
			if (array.Count == 4)
			{
				int value = (int)array[3].ToObject<int>();
				RightIsExpression = value == 1;
			}

			return new Condition
			{
				LeftValueStr = LeftValueStr,
				OperatorStr = OperatorStr,
				RightValue = RightValue,
				RightIsExpression = RightIsExpression,
			};
		}

		public override void WriteJson(JsonWriter writer, Condition? value, JsonSerializer serializer)
		{
			if (value == null) return;

			var array = new JArray
		{
			value.LeftValueStr,
			value.OperatorStr,
			value.RightValue
		};

			array.WriteTo(writer);
		}
	}

	public abstract class CompoundConditionConverter : JsonConverter<CompoundCondition>
	{

		public abstract CompoundCondition Create();
		public override CompoundCondition? ReadJson(JsonReader reader, Type objectType, CompoundCondition? existingValue, bool hasExistingValue, JsonSerializer serializer)
		{
			var target = Create();
			if (reader.TokenType == JsonToken.StartArray)
			{
				JArray array = JArray.Load(reader);
				if (array.Count > 0)
				{
					foreach (var child in array.Children())
					{
						try
						{
							Condition? condition = child.ToObject<Condition>();
							if (condition != null)
							{
								target.Conditions.Add(condition);
							}
							else
							{
								Debug.WriteLine("condition null ");
							}

						}
						catch (System.Exception e)
						{

							Debug.WriteLine(e.Message);
						}

					}
				}
			}
			else if (reader.TokenType == JsonToken.StartObject)
			{
				JObject jo = JObject.Load(reader);
				if (jo.Count > 0)
				{
					// 遍历所有属性
					foreach (var property in jo.Properties())
					{
						try
						{
							string key = property.Name; // 获取属性名
							JToken value = property.Value; // 获取属性值

							// 根据 key 进行后续处理
							if (key.StartsWith("or_"))
							{
								ConditionOr? conditionOr = value.ToObject<ConditionOr>();
								if (conditionOr != null)
								{
									target.ConditionOrs.Add(conditionOr);
								}
							}
							else if (key.StartsWith("and_"))
							{
								ConditionAnd? conditionAnd = value.ToObject<ConditionAnd>();
								if (conditionAnd != null)
								{
									target.ConditionAnds.Add(conditionAnd);
								}
							}
							else if (key.StartsWith("notAny_"))
							{
								ConditionNotAny? conditionNotAny = value.ToObject<ConditionNotAny>();
								if (conditionNotAny != null)
								{
									target.ConditionNotAnys.Add(conditionNotAny);
								}
							}
							else if (key.StartsWith("notAll_"))
							{
								ConditionNotAll? conditionNotAll = value.ToObject<ConditionNotAll>();
								if (conditionNotAll != null)
								{
									target.ConditionNotAlls.Add(conditionNotAll);
								}
							}
							else if (key.StartsWith("not_"))
							{
								ConditionNot? conditionNot = value.ToObject<ConditionNot>();
								if (conditionNot != null)
								{
									target.ConditionNots.Add(conditionNot);
								}
							}
							else
							{
								Condition? condition = value.ToObject<Condition>();
								if (condition != null)
								{
									target.Conditions.Add(condition);
								}
							}
						}
						catch (System.Exception e)
						{

							Debug.WriteLine(e.Message);
						}

					}
				}
			}


			return target;
		}

		public override void WriteJson(JsonWriter writer, CompoundCondition? value, JsonSerializer serializer)
		{
			// 直接使用默认序列化逻辑
			serializer.Serialize(writer, value);

		}

	}

	public class ConditionOrConverter : CompoundConditionConverter
	{
		public override CompoundCondition Create()
		{
			return new ConditionOr();
		}
	}

	public class ConditionAndConverter : CompoundConditionConverter
	{
		public override CompoundCondition Create()
		{
			return new ConditionAnd();
		}
	}

	public class ConditionNotConverter : CompoundConditionConverter
	{
		public override CompoundCondition Create()
		{
			return new ConditionNot();
		}
	}

	public class ConditionNotAnyConverter : CompoundConditionConverter
	{
		public override CompoundCondition Create()
		{
			return new ConditionNotAny();
		}
	}

	public class ConditionNotAllConverter : CompoundConditionConverter
	{
		public override CompoundCondition Create()
		{
			return new ConditionNotAll();
		}
	}
}
